<?php
/**
 * Default 2 player battle setup
 *
 */
class Default_Battle{
	
	
	/**
	 * Creates a new battle instance for the host
	 * Also Deploys default units for the host player in the NORTH
	 *
	 * @param array $request ['P'= PlayerId, 'Bn'=BattleName varchar(16), 'Pwd'= Password varchar(16) if required, otherwise empty string!)]
	 * @return array $return ['B'=NewBattleId]
	 */
	public static function createDefaultBattleInstanceForHost(array $request){
		 require_once(DirectoryManager::$pathModels."/battle/BattleCommand.php");
		 
		 // Create a new battle instance for a Host
		 $result = BattleCommand::createBattleInstance($request);
		 $newBattleId = $result['B'];
		 $newTimeStamp = $result['ts'];
		 
		 // Deploy Default Units for the Host (HARDCODED Team=T=1 for Host)
		 $newRequest = array("B"=>$newBattleId, "P"=>$request['P'], "Direction"=>"NORTH");
		 Default_Battle::addDefaultPlayerUnits($newRequest);
		 
		 // Return the new battle Id as 'B' in an array
		 $return = array("B"=>$newBattleId, "ts"=>$newTimeStamp);
		 return $return;
	}
	
	
	/**
	 * Deploys Default units for the target PlayerId in a given battle
	 * @param array $request ['P'=PlayerId, 'B'=BattleId, 'Direction'= Where Units are deployed (NORTH OR SOUTH)
	 */
	public static function addDefaultPlayerUnits(array $request){
		require_once(DirectoryManager::$pathModels."/unit/Unit_1T.php");

		//TODO Expand addDefaultPlayerUnits for up to 4 Players
		$playerId = $request['P'];
		$battleId = $request['B'];
		$direction = $request['Direction'];
		
		If($direction == "NORTH"){
			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>4, "y"=>1, "r"=>180);
			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>5, "y"=>1, "r"=>180);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>6, "y"=>1, "r"=>180);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>7, "y"=>1, "r"=>180);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>2, "P"=>$playerId, "x"=>5, "y"=>2, "r"=>180);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>2, "P"=>$playerId, "x"=>6, "y"=>2, "r"=>180);
//			Unit_1T::Create($unitRequest);
		}ElseIf($direction == "SOUTH"){
			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>4, "y"=>10, "r"=>0);
			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>5, "y"=>10, "r"=>0);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>6, "y"=>10, "r"=>0);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>1, "P"=>$playerId, "x"=>7, "y"=>10, "r"=>0);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>2, "P"=>$playerId, "x"=>5, "y"=>9, "r"=>0);
//			Unit_1T::Create($unitRequest);
//			$unitRequest = array("B"=>$battleId, "TYP"=>2, "P"=>$playerId, "x"=>6, "y"=>9, "r"=>0);
//			Unit_1T::Create($unitRequest);
		}Else{
			throw new exception("No default unit deployment defined for Direction '".$direction."'");
		}
	}
	
	
	
//	//=== OLD FUNCTION ===========================================================
	/**
//	 * HARDCODED: Creates a battle with deployed units for the Host
//	 *
//	 * @param array $request
//	 * @return array $allUnitArrays
//	 */
//	public function createDefaultBattle(array $request){
//		require_once(DirectoryManager::$pathModels."/battle/BattleCommand.php");
//		require_once(DirectoryManager::$pathModels."/unit/Unit_1T.php");
//
//		//Create a new battle instance(and retrieve battleId)
//		$return = BattleCommand::Create($request);
//		$newBattleId = $return['B']['NewBattleId'];
//		//throw new exception("DEBUG MARC: ".$newBattleId);
//
//		//Create Units for both teams
//		//	HOST (NORTH) HARDCODED: Player 1
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>1, "x"=>4, "y"=>1, "r"=>180);
//		$unitReturn1 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>1, "x"=>5, "y"=>1, "r"=>180);
//		$unitReturn2 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>1, "x"=>6, "y"=>1, "r"=>180);
//		$unitReturn3 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>1, "x"=>7, "y"=>1, "r"=>180);
//		$unitReturn4 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>2, "P"=>1, "x"=>5, "y"=>2, "r"=>180);
//		$unitReturn5 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>2, "P"=>1, "x"=>6, "y"=>2, "r"=>180);
//		$unitReturn6 = Unit_1T::Create($unitRequest);
//
////		//Populate the return array
////		$allUnitArrays = array($unitReturn1, $unitReturn2, $unitReturn3, $unitReturn4, $unitReturn5, $unitReturn6, $unitReturn7, $unitReturn8, $unitReturn9, $unitReturn10, $unitReturn11, $unitReturn12);
////
////		//Now the battle is populated, return the id
////		return array("players" => array(
////				array("id"=>1, "name"=>"Matt","team"=>1),
////				array("id"=>2, "name"=>"Marc","team"=>2)
////			),"units"=>$allUnitArrays
////		);
//		
//		//ONLY NEEDS TO RETURN THE NEW BATTLE ID
//		return $newBattleId;
//		
//		//	PLAYER 2 (SOUTH)
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>2, "x"=>4, "y"=>10, "r"=>0);
//		$unitReturn7 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>2, "x"=>5, "y"=>10, "r"=>0);
//		$unitReturn8 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>2, "x"=>6, "y"=>10, "r"=>0);
//		$unitReturn9 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>1, "P"=>2, "x"=>7, "y"=>10, "r"=>0);
//		$unitReturn10 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>2, "P"=>2, "x"=>5, "y"=>9, "r"=>0);
//		$unitReturn11 = Unit_1T::Create($unitRequest);
//		$unitRequest = array("B"=>$newBattleId, "TYP"=>2, "P"=>2, "x"=>6, "y"=>9, "r"=>0);
//		$unitReturn12 = Unit_1T::Create($unitRequest);
//	}
}
?>